package com.ry.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ry.dao.StateDao;
import com.ry.model.dto.*;
import com.ry.model.entity.State;
import com.ry.service.CommentServcie;
import com.ry.service.FollowerService;
import com.ry.service.StateService;
import com.ry.service.UserService;
import com.ry.utils.DateTimeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ry
 * @since 2021-11-11 10:32
 **/
@Slf4j
@Service
public class StateServiceImpl implements StateService {
    @Autowired
    private StateDao stateDao;
    @Autowired
    private CommentServcie commentServcie;
    @Autowired
    private FollowerService followerService;
    @Autowired
    private UserService userService;

    /**
     * 发表状态
     *
     * @param stateDTO
     */
    @Override
    public void publish(StateDTO stateDTO) {
        State entity = new State();
        entity.setId(stateDTO.getId());
        entity.setUserId(stateDTO.getUserId());
        entity.setContent(stateDTO.getContent());
        entity.setType(stateDTO.getType());
        entity.setImages(JSONUtil.toJsonStr(stateDTO.getImages()));
        entity.setVideo(stateDTO.getVideo());
        entity.setLocation(stateDTO.getLocation());
        entity.setPublishTime(LocalDateTime.now().toString());

        stateDao.insert(entity);
    }

    /**
     * 分页根据条件查询状态
     *
     * @param queryState
     * @return
     */
    @Override
    public Page<StateDetailDTO> selectByPage(QueryState queryState) {
        Page<State> page = new Page<>(queryState.getPageNum(),queryState.getPageSize());
        Page<StateDetailDTO> pageInfo = new Page<>(queryState.getPageNum(),queryState.getPageSize());

        QueryWrapper<State> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("publish_time");
        stateDao.selectPage(page, wrapper);

        List<StateDetailDTO> infoList =  page.getRecords()
                .stream()
                .map(state -> {
                    StateDetailDTO stateDetailDTO = new StateDetailDTO();
                    stateDetailDTO.setId(state.getId());
                    stateDetailDTO.setUser(userService.getUserById(state.getUserId()));
                    stateDetailDTO.setContent(state.getContent());
                    stateDetailDTO.setType(state.getType());
                    stateDetailDTO.setImages(JSONUtil.toList(state.getImages(),String.class));
                    stateDetailDTO.setVideo(state.getVideo());
                    stateDetailDTO.setLocation(state.getLocation());
                    stateDetailDTO.setPublishTime(DateTimeUtil.format1(state.getPublishTime()));
                    setFollowers(stateDetailDTO);
                    setComments(stateDetailDTO);

                    return stateDetailDTO;
                })
                .collect(Collectors.toList());


        pageInfo.setTotal(page.getTotal());
        pageInfo.setRecords(infoList);

        return pageInfo;
    }

    private void setComments(StateDetailDTO stateDetailDTO) {
        List<CommentDTO> commentDTOS = commentServcie.selectByStateId(stateDetailDTO.getId());
        stateDetailDTO.setComments(commentDTOS);
    }

    private void setFollowers(StateDetailDTO stateDetailDTO){
        Integer stateId = stateDetailDTO.getId();

        List<FollowerDTO> followerDTOS = followerService.selectByStateId(stateId);
        stateDetailDTO.setFollowers(followerDTOS);
    }

    /**
     * 根据id查询状态
     *
     * @param id
     * @return
     */
    @Override
    public StateDetailDTO selectById(Integer id) {
        State state = stateDao.selectById(id);
        StateDetailDTO stateDetailDTO = new StateDetailDTO();
        stateDetailDTO.setId(state.getId());
        stateDetailDTO.setUser(userService.getUserById(state.getUserId()));
        stateDetailDTO.setContent(state.getContent());
        stateDetailDTO.setType(state.getType());
        stateDetailDTO.setImages(JSONUtil.toList(state.getImages(),String.class));
        stateDetailDTO.setVideo(state.getVideo());
        stateDetailDTO.setLocation(state.getLocation());
        stateDetailDTO.setPublishTime(DateTimeUtil.format1(state.getPublishTime()));
        setFollowers(stateDetailDTO);
        setComments(stateDetailDTO);

        return stateDetailDTO;
    }

    /**
     * 根据id删除状态
     *
     * @param id
     */
    @Override
    public void deleteById(Integer id) {
        stateDao.deleteById(id);
    }
}
